package xiaodou.core.WeiboData
{
	import com.sina.microblog.WeiboService;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.events.WeiboServiceEvent;
	import com.weibo.charts.ui.dots.NullDot;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import mx.controls.Alert;
	
	import flashx.textLayout.elements.BreakElement;
	
	import spark.formatters.DateTimeFormatter;
	import spark.globalization.supportClasses.GlobalizationBase;
	
	import xiaodou.core.ObjectUtil;
	import xiaodou.core.WeiboData.GlobalWeiboDataModel;
	import xiaodou.core.WeiboData.WeiboFan;
	import xiaodou.core.WeiboData.WeiboFansInfo;
	import xiaodou.core.WeiboData.events.WeiboDataEvent;
	import xiaodou.core.WeiboData.events.WeiboEvent;
	import xiaodou.data.blogAccount.BlogAccountData;
	import xiaodou.core.WeiboData.events.WeiboErrorDesc;
	
	public class WeiboDataDirector extends EventDispatcher
	{
		protected static var _mb:WeiboService;//FLASH API 封装接口类
		protected static var _mbForAuthorize:WeiboService;//FLASH API 封装接口类
		protected static var _AuthorizeResult:BlogAccountData = new BlogAccountData();
		
		private static var _getFansCount:int = 0;
		private static var _getFansTagCount:int = 0;
		private static var _getFansAPICallTimes:int = 0;
		private static var _getFansTagAPICallTimes:int = 0;
		private static var _getStatusDate:Date = new Date();
		private static var _getCommentsDate:Date = new Date();
		
		private static var _getOneWeekCommentsToMeDate:Date = new Date();
		private static var _getOneWeekCommentsMentionMeDate:Date = new Date();
		private static var _getOneWeekStatusMentionMeDate:Date = new Date();
		private static var _getOneWeekStatusDate:Date = new Date();
		
		private static var _getOneWeekCommentsToMeCallTime:int = 0;
		private static var _getOneWeekCommentsMentionMeCallTime:int = 0;
		private static var _getOneWeekStatusMentionMeCallTime:int = 0;
		private static var _getOneWeekStatusDateCallTime:int = 0;
		
		private    var _MaxCommentsToMeCount:int = 300;
		private    var _MaxCommentsMentionMeCount:int = 300;
		private    var _MaxStatusMentionMeCount:int = 300;
		private    var _MaxStatusCount:int = 300;
		
		private static var _aryFansIDs:Array = new Array();
		protected var DateFormat:DateTimeFormatter = new DateTimeFormatter();
		
		
		public static function set token(value:String):void
		{
			if(_mb == null)
			{
				_mb = new WeiboService("2527812079", "cb5db8c7c10403cf320c945949f16abc", "http://bravestarrsammy.sinaapp.com/proxy.php");	
			}
			_mb.setNewToken(value);
			
			trace("new Token:" + value);
			GlobalWeiboDataModel.CommentsInfoModel.RemoveAllData();
			GlobalWeiboDataModel.FansInfoModel.RemoveAllData();
			GlobalWeiboDataModel.StatusInfoModel.RemoveAllData();
			
			_getOneWeekCommentsToMeCallTime = 0;
			_getOneWeekCommentsMentionMeCallTime= 0;
			_getOneWeekStatusMentionMeCallTime = 0;
			_getOneWeekStatusDateCallTime = 0;
			
			new WeiboDataDirector().GetUserInfo();
			
		}
		
		public function WeiboDataDirector()
		{
			if(_mb == null)
				_mb = new WeiboService("2527812079", "cb5db8c7c10403cf320c945949f16abc", "http://bravestarrsammy.sinaapp.com/proxy.php");	
			
			DateFormat.dateTimePattern = "yyyy-MM-dd";
			//_getStatusDate = DateFormat.format(_getStatusDate);
		}
		
		/**
		 * 登录，弹出授权框 
		 * 
		 */		
		public function Login():void
		{
			_mb.login();
			_mb.addEventListener(MicroBlogEvent.LOGIN_RESULT, OnLoginResult);
			_mb.addEventListener(MicroBlogErrorEvent.LOGIN_ERROR, OnLoginError);
		}
		
		protected function OnLoginError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(MicroBlogEvent.LOGIN_RESULT, OnLoginResult);
			_mb.removeEventListener(MicroBlogErrorEvent.LOGIN_ERROR, OnLoginError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.LOGIN_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
		}
		
		/**
		 * 弹出授权框，强制用户手动输入账号密码授权应用 
		 * 事件监听对象:WeiboDataDirector
		 * 成功抛出:AUTHORIZE_RESULT
		 * 失败抛出:AUTHORIZE_ERROR
		 * 在事件的 result对象的JSON结构如 {"access_token":"2.0xxxxxxxxxx", "expires_in":"96760","UID":"13465456546"}
		 */		
		public function Authorize():void
		{
			if(_mbForAuthorize == null)
				_mbForAuthorize = new WeiboService("2527812079", "cb5db8c7c10403cf320c945949f16abc","http://naked.sinaapp.com/proxy.php");

			//_mbForAuthorize.Authorize();
			_mbForAuthorize.login(null, null, true);
			_mbForAuthorize.addEventListener(MicroBlogEvent.LOGIN_RESULT, OnAuthorizeResult);
			_mbForAuthorize.addEventListener(MicroBlogErrorEvent.LOGIN_ERROR, OnAuthorizeLoginError);
		}
		
		protected function OnAuthorizeLoginError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mbForAuthorize.removeEventListener(MicroBlogEvent.LOGIN_RESULT, OnAuthorizeResult);
			_mbForAuthorize.removeEventListener(MicroBlogErrorEvent.LOGIN_ERROR, OnAuthorizeLoginError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.AUTHORIZE_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
		}
		
		protected function OnAuthorizeResult(e:MicroBlogEvent):void
		{
			_mbForAuthorize.removeEventListener(MicroBlogEvent.LOGIN_RESULT, OnAuthorizeResult);
			_mbForAuthorize.removeEventListener(MicroBlogErrorEvent.LOGIN_ERROR, OnAuthorizeLoginError);
			
			var today:Date = new Date();
			
			_AuthorizeResult.accessToken = e.result["access_token"].toString();
			_AuthorizeResult.expireIn  = new Date(today.time + e.result["expires_in"]*1000);
			
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.AUTHORIZE_RESULT);
			_mbForAuthorize.getUID();
			_mbForAuthorize.addEventListener(WeiboServiceEvent.GET_UID_RESULT, OnAuthorizeUIDResult);
			_mbForAuthorize.addEventListener(WeiboServiceEvent.GET_UID_ERROR, OnAuthorizeError);
			
		}
		
		protected function OnAuthorizeUIDResult(e:MicroBlogEvent):void
		{
			_mbForAuthorize.removeEventListener(WeiboServiceEvent.GET_UID_RESULT, OnAuthorizeUIDResult);
			_mbForAuthorize.removeEventListener(WeiboServiceEvent.GET_UID_ERROR, OnAuthorizeError);
			
			_AuthorizeResult.id =  e.result["uid"];
			
			_mbForAuthorize.usersShow(_AuthorizeResult.id.toString());
			_mbForAuthorize.addEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnAuthorizeUserShowResult);
			_mbForAuthorize.addEventListener(WeiboServiceEvent.GET_UID_ERROR, OnAuthorizeError);
			
		}
		
		protected function OnAuthorizeUserShowResult(e:MicroBlogEvent):void
		{
			_mbForAuthorize.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnAuthorizeUserShowResult);
			_mbForAuthorize.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnAuthorizeError);
			
			_AuthorizeResult.account = e.result["screen_name"].toString();
			
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.AUTHORIZE_RESULT);
			Evt.result = _AuthorizeResult;
			dispatchEvent(Evt);
		}
		
		protected function OnAuthorizeError(e:MicroBlogErrorEvent):void
		{
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.AUTHORIZE_ERROR);
			Evt.result = e.message;
			dispatchEvent(Evt);
		}
		
		/**
		 * 获得用户信息
		 * 事件监听对象:GlobalWeiboDataModel.AccountModel
		 * 成功抛出:WeiboDataEvent.ACCOUNT_REFRESH_SUCCESS
		 * 失败抛出:WeiboDataEvent.ACCOUNT_REFRESH_ERROR
		 * 收到成功事件后，直接取GlobalWeiboDataModel.AccountModel各项属性值即可
		 */			
		public function GetUserInfo():void
		{
			_mb.getUID();
			_mb.addEventListener(WeiboServiceEvent.GET_UID_RESULT, OnCustomGetUID);
			_mb.addEventListener(WeiboServiceEvent.GET_UID_ERROR, OnGetUserInfoError);
		}
		
		protected function OnGetUserInfoError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.GET_UID_RESULT, OnCustomGetUID);
			_mb.removeEventListener(WeiboServiceEvent.GET_UID_ERROR, OnGetUserInfoError);
			
			var event:WeiboEvent  = new WeiboEvent(WeiboDataEvent.ACCOUNT_REFRESH_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
			
			Alert.show(event.message);
		}		
		
		protected function OnCustomGetUID(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.GET_UID_RESULT, OnCustomGetUID);
			_mb.removeEventListener(WeiboServiceEvent.GET_UID_ERROR, OnGetUserInfoError);
			
			GlobalWeiboDataModel.AccountModel.UID = e.result["uid"];
			
			_mb.usersShow(e.result["uid"].toString());
			_mb.addEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnCustomUserShowResult);
			_mb.addEventListener(WeiboServiceEvent.USERS_SHOW_ERROR, OnCustomUserShowError);
		}
		
		protected function OnCustomUserShowError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnCustomUserShowResult);
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_ERROR, OnCustomUserShowError);
			
			var event:WeiboEvent  = new WeiboEvent(WeiboDataEvent.ACCOUNT_REFRESH_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
			Alert.show("获取用户数据错误：" + event.message);
		}
		
		protected function OnCustomUserShowResult(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnCustomUserShowResult);
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_ERROR, OnCustomUserShowError);
			
			ObjectUtil.fill(e.result, GlobalWeiboDataModel.AccountModel);
			GlobalWeiboDataModel.AccountModel.bRefresh = true;
			
		}
		/**
		 *  批量获取用户的粉丝数、关注数、微博数 
		 * @param UIDs 需要获取数据的用户UID，多个之间用逗号分隔，最多不超过100个。
		 * 
		 */		
		public function GetUserCounts(UIDs:String):void
		{
			_mb.usersCounts(UIDs);
			_mb.addEventListener(WeiboServiceEvent.USERS_COUNTS_RESULT, OnUserCountResult);
			_mb.addEventListener(WeiboServiceEvent.USERS_COUNTS_ERROR, OnGetUserCountError);
			
		}
		
		protected function OnGetUserCountError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.USERS_COUNTS_RESULT, OnUserCountResult);
			_mb.removeEventListener(WeiboServiceEvent.USERS_COUNTS_ERROR, OnGetUserCountError);
			
			var event:WeiboEvent  = new WeiboEvent(WeiboDataEvent.USER_COUNTS_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
			Alert.show("获取用户数据错误：" + event.message);
		}
		
		/**
		 *获得活跃粉丝数目 
		 * 
		 */		
		public function GetActiveFollowers():void
		{
			_mb.friendshipsFollowersActive(GlobalWeiboDataModel.AccountModel.UID);
			_mb.addEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ACTIVE_RESULT, OnGetActiveFollowers);
			_mb.addEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ACTIVE_ERROR, OnGetActiveFollowersError);
		}
		
		protected function OnGetActiveFollowersError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ACTIVE_RESULT, OnGetActiveFollowers);
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ACTIVE_ERROR, OnGetActiveFollowersError);
			
			var event:WeiboEvent  = new WeiboEvent(WeiboDataEvent.ACTIVE_FOLLOWER_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
			Alert.show("获取活跃粉丝错误：" + event.message);
		}
		
		protected function OnGetActiveFollowers(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ACTIVE_RESULT, OnGetActiveFollowers);
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ACTIVE_ERROR, OnGetActiveFollowersError);
			
			var length:int = e.result["users"]["length"];
			var aryTemp:Array = new Array();
			for(var i:int = 0; i < length; ++i)
			{
				var fan:WeiboFan = new WeiboFan();
				ObjectUtil.fill(e.result["users"][i], fan);
				aryTemp.push(fan);
			}
			
			GlobalWeiboDataModel.FansInfoModel.AryActiveFollowers = aryTemp;
		}
		
		
		/**
		 * 发布一条新微博
		 *  <br/>事件监听者:GlobalWeiboDataModel.StatusInfoMode
		 *  <br/>成功抛出WeiboDataEvent.UPDATE_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.UPDATE_ERROR
		 * @param msg 发布微博的内容
		 * @return 
		 *  
		 */		
		public function UpdateStatus(msg:String):void
		{
			var statusMsg:String = encodeURI(msg);
			_mb.update(statusMsg);
			_mb.addEventListener(WeiboServiceEvent.UPDATE_RESULT, OnUpdateStatusSuccess);
			_mb.addEventListener(WeiboServiceEvent.UPDATE_ERROR, OnWeiboServiceResultError);
		}
		
		protected function OnUpdateStatusSuccess(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.UPDATE_RESULT, OnUpdateStatusSuccess);
			
			GlobalWeiboDataModel.StatusInfoModel.UpdateStatusSuccess();
		}
		
		
		protected function OnWeiboServiceResultError(e:MicroBlogErrorEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.UPDATE_RESULT, OnUpdateStatusSuccess);
			_mb.removeEventListener(WeiboServiceEvent.UPDATE_ERROR, OnWeiboServiceResultError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.UPDATE_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
			Alert.show("发布微博错误：" + event.message);
		}
		
		/**
		 *查询粉丝的详细信息
		 * <br>事件监听者:GlobalWeiboDataModel.FansInfoModel
		 * <br/>成功抛出WeiboDataEvent.GET_FANS_INFO_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.GET_FANS_INFO_ERROR
		 * @param ID：
		 * 
		 */		
		public function GetUserInfoByID(id:String):void
		{
			_mb.usersShow(id);	
			_mb.addEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnGetOneUserInfo);
			_mb.addEventListener(WeiboServiceEvent.USERS_SHOW_ERROR,  OnGetOneUserInfoError);
		}
		
		protected function OnGetOneUserInfoError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnGetOneUserInfo);
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_ERROR, OnGetOneUserInfoError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.GET_FANS_INFO_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			
			dispatchEvent(event);
			Alert.show("查询粉丝信息错误：" + event.message);
		}
		
		protected function OnGetOneUserInfo(e:MicroBlogEvent):void
		{	
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnGetOneUserInfo);
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_ERROR, OnGetOneUserInfoError);
			trace("OnGetOneUserInfo:" + e.result["name"].toString());
			GlobalWeiboDataModel.FansInfoModel.PushFollower(e.result);
			
		}
		
		/**
		 * 批量获得粉丝详细信息，现在暂时需要获得1000个，由于API限制，每次获得200个粉丝后，返回数据 
		 *  <br>事件监听者:GlobalWeiboDataModel.FansInfoModel
		 * 	<br/>成功抛出WeiboDataEvent.FANS_ANALYTICS_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.FANS_ANALYTICS_ERROR
		 * @param ID：
		 * @return 
		 * 
		 */		
		public function GetFansInfoBatch(FansCount:int = 1000):void
		{
			_getFansAPICallTimes = FansCount/200 + (FansCount%200 > 0 ? 1 : 0);
			_mb.friendshipsFollowers(GlobalWeiboDataModel.AccountModel.UID, GlobalWeiboDataModel.AccountModel.screen_name, 200);
			_getFansCount = 1;
			_mb.addEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS, OnGetFansBatch);
			_mb.addEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ERROR, OnGetFansBatchError);
		}
		
		protected function OnGetFansBatchError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS, OnGetFansBatch);
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ERROR, OnGetFansBatchError);
			
			_getFansCount = 0;
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.FANS_ANALYTICS_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			dispatchEvent(event);
			
			Alert.show("获得粉丝数据错误：" + event.message);
		}
		
		protected function OnGetFansBatch(e:MicroBlogEvent):void
		{	
			GlobalWeiboDataModel.FansInfoModel.PushFollowersBatch(e.result["users"]);
			if(_getFansCount < _getFansAPICallTimes)
			{
				_mb.friendshipsFollowers(GlobalWeiboDataModel.AccountModel.UID, GlobalWeiboDataModel.AccountModel.screen_name, 200, e.result["next_cursor"]);
				_getFansCount++;
			}
			else
			{
				_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS, OnGetFansBatch);
				_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_ERROR, OnGetFansBatchError);
				_getFansCount = 0;
			}
		}
		
		/**
		 * 获得当前授权access_token信息 
		 *  <br>事件监听者:
		 * 	<br/>成功抛出WeiboDataEvent.FANS_ANALYTICS_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.FANS_ANALYTICS_ERROR
		 * 	 * var obj:Object = {
		 create_at :创建日期
		 expire_in:还有多少秒过期
		 expire_date:过期时间
		 UID       :账号UID
		 };
		 * @param FansCount
		 * 
		 */		
		
		public function GetTokenInfo(token:String):void
		{
			_mb.GetTokenInfo(token);
			_mb.addEventListener(WeiboServiceEvent.GET_TOKEN_IFNO_RESULT, OnGetTokenInfo);
			_mb.addEventListener(WeiboServiceEvent.GET_TOKEN_IFNO_ERROR,OnApiResultError);
		}
		
		
		public function AccessToekn():void
		{
			//_mb.AccessToken();
			//_mb.addEventListener(WeiboServiceEvent.ACCESS_TOKEN_RESULT, OnGetTokenInfo);
			//_mb.addEventListener(WeiboServiceEvent.ACCESS_TOKEN_ERROR,OnApiResultError);
		}
		
		/**
		 *批量获得粉丝的标签，由于API的限制，一次只能取20个,请勿请求太多粉丝，根据网络情况而定。。 
		 * 最多不超过500个
		 *   <br>事件监听者:GlobalWeiboDataModel.AccountModel
		 * 	<br/>成功抛出WeiboDataEvent.TOKEN_INFO_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.TOKEN_INFO_ERROR
		 * @param FansCount
		 * @return 
		 * 
		 */		
		public function GetFansTagsBatch(FansCount:int = 200):void
		{
			if(FansCount > 500)
			{
				trace("不能超过500！");
				return;
			}
			
			_aryFansIDs.splice(0);
			if(FansCount > GlobalWeiboDataModel.AccountModel.followers_count)
				FansCount = GlobalWeiboDataModel.AccountModel.followers_count;
			
			_getFansTagAPICallTimes = FansCount/20 + (FansCount%20 > 0 ? 1 : 0);
			
			_mb.friendshipsFollowersIDs(GlobalWeiboDataModel.AccountModel.UID,null, FansCount);
			_mb.addEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_IDS, OnForFansTagsBatch);
			_mb.addEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_IDS_ERROR, OnForFansTagsBatchError);
		}
		
		protected function OnForFansTagsBatch(e:MicroBlogEvent):void
		{
			_aryFansIDs = e.result["ids"];
			trace(_aryFansIDs.length.toString());
			
			var nIDCount:int = (_aryFansIDs.length > 20 ? 20 : _aryFansIDs.length);
			
			var ids:String = new String();
			for(var i:int = 0; i < nIDCount; i++)
			{
				ids += _aryFansIDs[i].toString();
				if(i != (nIDCount-1))
					ids += ",";
			}
			
			_mb.tagsBatch(ids);
			_mb.addEventListener(WeiboServiceEvent.TAGS_BATCH_RESULT, OnFansTagsBatch);
			_getFansTagCount = 1;
		}
		
		protected function OnFansTagsBatch(e:MicroBlogEvent):void	
		{
			for(var i:int = 0; i < e.result["length"]; ++i)
			{
				for(var j:int = 0; j < e.result[i]["tags"]["length"];++j)
				{
					GlobalWeiboDataModel.FansInfoModel.PushFansTag(e.result[i]["tags"][j]);
				}
			}
			
			if(_getFansTagCount < _getFansTagAPICallTimes)
			{
				var fansIDCount:int = 20;
				var bLastTime:Boolean = false;
				if(_getFansTagCount == _getFansTagAPICallTimes - 1)//最后一次
				{
					fansIDCount = _aryFansIDs.length % 20;
					bLastTime = true;
				}
				
				var ids:String = new String();
				for(var k:int = _getFansTagCount*20; k < fansIDCount + _getFansTagCount*20; k++)
				{
					ids += _aryFansIDs[k].toString();
					if(k != ( fansIDCount +  _getFansTagCount*20 - 1))
						ids += ",";
				}
				
				_getFansTagCount++;
				
				if(ids != "")
				{
					_mb.tagsBatch(ids);
					_mb.addEventListener(WeiboServiceEvent.TAGS_BATCH_RESULT, OnFansTagsBatch);
				}
				else
				{
					if(!bLastTime)
						OnFansTagsBatch(e);
				}
				
				if(bLastTime)
				{
					GlobalWeiboDataModel.FansInfoModel.FansTagCountIsReady();
					_mb.removeEventListener(WeiboServiceEvent.TAGS_BATCH_RESULT, OnFansTagsBatch);
					_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_IDS_ERROR, OnApiResultError);
				}
				
			}
			trace("_getFansTagCount" + _getFansTagCount.toString());
			trace("_getFansTagAPICallTimes" + _getFansTagAPICallTimes.toString());
		}
		
		protected function OnForFansTagsBatchError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_IDS, OnForFansTagsBatch);
			_mb.removeEventListener(WeiboServiceEvent.FRIENDSHIPS_FOLLOWERS_IDS_ERROR, OnForFansTagsBatchError);
				
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.FANS_TAG_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			dispatchEvent(event);
			
			Alert.show("获得粉丝标签数据错误：" + event.message);
			
		}
		
		protected function OnGetTokenInfo(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.GET_TOKEN_IFNO_RESULT, OnGetTokenInfo);
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.TOKEN_INFO_SUCCESS);
			
			var d:Date = new Date(1970);
			var CreateDate:Date = new Date(d.time + e.result["create_at"]*1000);
			var expireDate:Date = new Date(new Date().time + e.result["expire_in"]*1000);
			
			var obj:Object = {
				create_at :CreateDate,
				expire_in:e.result["expire_in"],
					expire_Date:expireDate,
					User_ID    :e.result["uid"]
			};
			event.result = obj;
			
			GlobalWeiboDataModel.AccountModel.OnGetTokenSuccess(event);
		}
		
		/**
		 * 请求最近一周的发微博数
		 *  <br>事件监听者:GlobalWeiboDataModel.StatusInfoModel
		 * 	<br/>成功抛出WeiboDataEvent.GET_ONEDAY_STATUS_COUNTS_SUCCESS,接收到这个事件后，直接调用public function GetOneDayStatusCounts(d:Date):Object取数据
		 *  <br/>失败抛出WeiboDataEvent.GET_ONEDAY_STATUS_COUNTS_ERROR
		 * 
		 */		
		public function GetOneWeekStatus():void
		{
			
		}
		
		/**
		 * 请求最近一周的我收到的评论数
		 *  <br>事件监听者:GlobalWeiboDataModel.CommentsInfoModel
		 * 	<br/>成功抛出WeiboDataEvent.GET_ONEWEEK_COMMENTS_TO_ME_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.GET_ONEWEEK_COMMENTS_TO_ME_ERROR
		 * 
		 */	
		public function GetOneWeekCommentsToMe():void
		{
			_getOneWeekCommentsToMeDate = new Date(new Date().time - WeiboFansInfo.millisecondsPerDay*7);//一周之前的时间点
			if(_getOneWeekCommentsToMeDate < GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TimeCursorBack //时间超过了最前面，需要继续取更久远的微博
				&& GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TotalNum < _MaxCommentsToMeCount)//并且没有超过最大抓取数目
			{
				var MaxID:String = GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TimeCursorBackID;
				if(MaxID == "-1")
					MaxID = "0";
				
				_mb.commentsToMe("0",MaxID,50, _getOneWeekCommentsToMeCallTime+1);
				_getOneWeekCommentsToMeCallTime++;
				_mb.addEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneWeekCommentsToMe);
				_mb.addEventListener(WeiboServiceEvent.COMMENTS_TO_ME_ERROR,  OnGetOneWeekCommentsToMeError);
			}
			else//已经有数据，直接抛事件
			{
				GlobalWeiboDataModel.CommentsInfoModel.OneWeekCommentsToMeReady();
				_getOneWeekCommentsToMeCallTime = 0;
			}
			
		}
		
		protected function OnGetOneWeekCommentsToMeError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneWeekCommentsToMe);
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_ERROR,  OnGetOneWeekCommentsToMeError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.GET_ONEWEEK_COMMENTS_TO_ME_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			dispatchEvent(event);
			
			_getOneWeekCommentsToMeCallTime = 0;
			
			Alert.show("获取用户一周收到的评论数据错误：" + event.message);
		}
		
		protected function OnGetOneWeekCommentsToMe(e:MicroBlogEvent):void
		{
			var length:int = int(e.result["comments"]["length"]);
			var i:int = 0;
			for(i = 0; i < length; ++i)
			{
				GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.PushNewComment(e.result["comments"][i]);
			}
			
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneWeekCommentsToMe);
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_ERROR,  OnGetOneWeekCommentsToMeError);
			
			GetOneWeekCommentsToMe();
		}
		/**
		 * 请求最近一周的@我的评论
		 *  <br>事件监听者:GlobalWeiboDataModel.CommentsInfoModel
		 * 	<br/>成功抛出WeiboDataEvent.GET_ONEWEEK_COMMENTS_MENTION_ME_SUCCESS,
		 *  <br/>失败抛出WeiboDataEvent.GET_ONEWEEK_COMMENTS_MENTION_ME_ERROR
		 * 
		 */	
		public function GetOneWeekCommentsMentionMe():void
		{
			_getOneWeekCommentsMentionMeDate = new Date(new Date().time - WeiboFansInfo.millisecondsPerDay*7);//一周之前的时间点
			if(_getOneWeekCommentsMentionMeDate < GlobalWeiboDataModel.CommentsInfoModel._CommentsMentionMeData.TimeCursorBack)//时间超过了最前面，需要继续取更久远的微博
			{
				var MaxID:String = GlobalWeiboDataModel.CommentsInfoModel._CommentsMentionMeData.TimeCursorBackID;
				if(MaxID == "-1")
					MaxID = "0";
				_mb.commentsMentions("0",MaxID,50,_getOneWeekCommentsMentionMeCallTime+1);
				_getOneWeekCommentsMentionMeCallTime++;
				_mb.addEventListener(WeiboServiceEvent.COMMENTS_MENTIONS_RESULT, OnGetOneWeekMentionMe);
				_mb.addEventListener(WeiboServiceEvent.COMMENTS_MENTIONS_ERROR, OnGetOneWeekMentionMeError);
			}
			else//已经有数据，直接抛事件
			{
				_getOneWeekCommentsMentionMeCallTime = 0;
				GlobalWeiboDataModel.CommentsInfoModel.OneWeekCommentsMentionMeReady();
			}
		}
		
		protected function OnGetOneWeekMentionMeError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_MENTIONS_RESULT, OnGetOneWeekMentionMe);
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_MENTIONS_ERROR,  OnGetOneWeekMentionMeError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.GET_ONEWEEK_COMMENTS_TO_ME_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			dispatchEvent(event);
			
			_getOneWeekCommentsMentionMeCallTime = 0;
			
			Alert.show("获取用户一周@我的评论数据错误：" + event.message);
		}
		
		protected function OnGetOneWeekMentionMe(e:MicroBlogEvent):void
		{
			var length:int = int(e.result["comments"]["length"]);
			var i:int = 0;
			for(i = 0; i < length; ++i)
			{
				GlobalWeiboDataModel.CommentsInfoModel._CommentsMentionMeData.PushNewComment(e.result["comments"][i]);
			}
			
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneWeekMentionMe);
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_MENTIONS_ERROR,  OnGetOneWeekMentionMeError);
			
			GetOneWeekCommentsMentionMe();
		}
		
		/**
		 * 请求最近一周的@我的微博
		 *  <br>事件监听者:GlobalWeiboDataModel.StatusInfoModel
		 * 	<br/>成功抛出WeiboDataEvent.GET_ONEWEEK_STATUS_MENTION_ME_SUCCESS
		 *  <br/>失败抛出WeiboDataEvent.GET_ONEWEEK_STATUS_MENTION_ME_ERROR
		 * @ date format :"yyyy-MM-dd"; 
		 * 
		 */	
		public function GetOneWeekStatusMentionMe():void
		{
			_getOneWeekStatusMentionMeDate = new Date(new Date().time - WeiboFansInfo.millisecondsPerDay*7);//一周之前的时间点
			if(_getOneWeekStatusMentionMeDate < GlobalWeiboDataModel.StatusInfoModel._StatusMentionMe.TimeCursorBack && GlobalWeiboDataModel.StatusInfoModel._StatusMentionMe.TotalNum < _MaxStatusMentionMeCount)//时间超过了最前面，需要继续取更久远的微博
			{
				trace(GlobalWeiboDataModel.StatusInfoModel._StatusMentionMe.TimeCursorBack.toString());
				var max_ID:String = GlobalWeiboDataModel.StatusInfoModel._StatusMentionMe.TimeCursorBackID;
				if(max_ID == "-1")
					max_ID = "0";
				
				_mb.mentions("0",max_ID.toString(),50,_getOneWeekStatusMentionMeCallTime+1);
				_getOneWeekStatusMentionMeCallTime++;
				_mb.addEventListener(WeiboServiceEvent.MENTIONS_RESULT, OnGetOneWeekStatusMentionMe);
				_mb.addEventListener(WeiboServiceEvent.MENTIONS_ERROR, OnGetOneWeekStatusMentionMeError);
			}
			else//已经有数据，直接抛事件
			{
				_getOneWeekStatusMentionMeCallTime = 0;
				GlobalWeiboDataModel.StatusInfoModel.OneWeekStatusToMeReady();
			}
		}
		
		protected function OnGetOneWeekStatusMentionMeError(e:MicroBlogErrorEvent):void
		{
			// TODO Auto-generated method stub
			_mb.removeEventListener(WeiboServiceEvent.MENTIONS_RESULT, OnGetOneWeekStatusMentionMe);
			_mb.removeEventListener(WeiboServiceEvent.MENTIONS_ERROR, OnGetOneWeekStatusMentionMeError);
			
			var event:WeiboEvent = new WeiboEvent(WeiboDataEvent.GET_ONEWEEK_STATUS_MENTION_ME_ERROR);
			event.message = WeiboErrorDesc.GetErrorDescByCode(e.code);
			dispatchEvent(event);
			
			_getOneWeekStatusMentionMeCallTime = 0;
			
			Alert.show("获取用户一周提到我的微薄错误：" + event.message);
		}
		
		public function OnGetOneWeekStatusMentionMe(e:MicroBlogEvent):void
		{
			var length:int = int(e.result["statuses"]["length"]);
			var i:int = 0;
			for(i = 0; i < length; ++i)
			{
				GlobalWeiboDataModel.StatusInfoModel._StatusMentionMe.PushNewStatus(e.result["statuses"][i]);
			}
			
			_mb.removeEventListener(WeiboServiceEvent.MENTIONS_RESULT, OnGetOneWeekStatusMentionMe);
			_mb.removeEventListener(WeiboServiceEvent.MENTIONS_ERROR, OnGetOneWeekStatusMentionMeError);
			
			GetOneWeekStatusMentionMe();
		}
		
		
		/**
		 * 请求某一天的发微博数
		 *  <br>事件监听者:GlobalWeiboDataModel.StatusInfoModel
		 * 	<br/>成功抛出WeiboDataEvent.GET_ONEDAY_STATUS_COUNTS_SUCCESS,接收到这个事件后，直接调用public function GetOneDayStatusCounts(d:Date):Object取数据
		 *  <br/>失败抛出WeiboDataEvent.GET_ONEDAY_STATUS_COUNTS_ERROR
		 * @ date format :"yyyy-MM-dd"; 
		 * 
		 */		
		public function GetOneDayStatusCounts(d:Date):void
		{
			if(d > new Date())
			{
				trace("不能获得未来的微博数据");
				return;
			}
			
			
			var FormatDate:Date = new Date(DateFormat.format(d));
			if(d < GlobalWeiboDataModel.StatusInfoModel._StatusSendbyMe.TimeCursorBack)//时间超过了最前面，需要继续取更久远的微博
			{
				var today:Date = new Date();
				if(d < new Date(today.getTime() - 30* WeiboFansInfo.millisecondsPerDay))
				{
					trace("最多一个月的数据！");
					return;
				}
				
				_getStatusDate = d;
				_mb.userTimeline(null,null,0, int(GlobalWeiboDataModel.StatusInfoModel._StatusSendbyMe.TimeCursorBackID));
				_mb.addEventListener(WeiboServiceEvent.USER_TIMELINE_RESULT, OnGetOneDayStatusCount);
			}
			else if(d > GlobalWeiboDataModel.StatusInfoModel._StatusMentionMe.TimeCursorFront)//时间超过了最新，需要取更新的微博
			{
				_getStatusDate = d;
				_mb.userTimeline(null,null,int(GlobalWeiboDataModel.StatusInfoModel._StatusSendbyMe.TimeCursorFrontID));
				_mb.addEventListener(WeiboServiceEvent.USER_TIMELINE_RESULT, OnGetOneDayStatusCount);
			}
			else
			{
				GlobalWeiboDataModel.StatusInfoModel.OneDayStatusCountsReady();
			}
		}
		
		
		/**
		 * 请求某一天的收到的评论数
		 *  <br>事件监听者:GlobalWeiboDataModel.WeiboCommentsInfo
		 * 	<br/>成功抛出WeiboDataEvent.GET_ONEDAY_COMMENTS_COUNTS_SUCCESS,接收到这个事件后，直接调用public function GetOneDayCommentCounts(d:Date):Object取数据
		 *  <br/>失败抛出WeiboDataEvent.GET_ONEDAY_STATUS_COUNTS_ERROR
		 * @ date format :"yyyy-MM-dd"; 
		 * 
		 */		
		public function GetOneDayCommentsCounts(d:Date):void
		{
			if(d > new Date())
			{
				trace("不能获得未来的微博数据");
				return;
			}
			
			var today:Date = new Date();
			if(d < new Date(today.getTime() - 30* WeiboFansInfo.millisecondsPerDay))
			{
				trace("最多一个月的数据！");
				return;
			}
			
			var FormatDate:Date = new Date(DateFormat.format(d));
			if(d < GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TimeCursorBack)//时间超过了最前面，需要继续取更久远的微博
			{
				_getCommentsDate = d;
				_mb.commentsToMe("0",GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TimeCursorBackID);
				_mb.addEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneDayCommentCount);
			}
			else if(d > GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TimeCursorFront)//时间超过了最新，需要取更新的微博
			{
				_getCommentsDate = d;
				_mb.commentsToMe(GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.TimeCursorFrontID);
				_mb.addEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneDayCommentCount);
			}
			else
			{
				GlobalWeiboDataModel.CommentsInfoModel.OneDayCommentsCountsReady();
			}
		}
		
		protected function OnGetOneDayCommentCount(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnGetOneDayCommentCount);
		}
		
		protected function OnGetOneDayStatusCount(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.USER_TIMELINE_RESULT, OnGetOneDayStatusCount);
			var length:int = int(e.result["statuses"]["length"]);
			var i:int = 0;
			for(i = 0; i < length; ++i)
			{
				GlobalWeiboDataModel.StatusInfoModel._StatusSendbyMe.PushNewStatus(e.result["statuses"][i]);
			}
			
			GetOneDayStatusCounts(_getStatusDate);
		}
		
		
		protected function OnUserCountResult(e:MicroBlogEvent):void
		{
			
		}
		
		
		protected function OnCommentsToMe(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnCommentsToMe);
			
			var length:int = int(e.result["comments"]["length"]);
			var i:int = 0;
			for(i = 0; i < length; ++i)
			{
				GlobalWeiboDataModel.CommentsInfoModel._CommentsToMeData.PushNewComment(e.result["comments"][i]);
			}
		}
		
		protected function OnUserTimeLineIDs(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.USER_TIMELINE_IDS_RESULT, OnUserTimeLineIDs);
		}
		
		
		protected function OnLoginResult(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(MicroBlogEvent.LOGIN_RESULT, OnLoginResult);
			
			_mb.getUID();
			_mb.addEventListener(WeiboServiceEvent.GET_UID_RESULT, OnGetUIDSuccess);
			_mb.addEventListener(WeiboServiceEvent.GET_UID_ERROR, OnApiResultError);
			
			dispatchEvent(e.clone());
			
		}
		
		protected function OnGetUIDSuccess(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.GET_UID_RESULT, OnGetUIDSuccess);
			GlobalWeiboDataModel.AccountModel.UID = e.result["uid"];
			trace("OnGetUIDSuccess: " + e.result["uid"]); 
			
			_mb.usersShow(GlobalWeiboDataModel.AccountModel.UID);
			_mb.addEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnUserShowResult);
			_mb.addEventListener(WeiboServiceEvent.USERS_SHOW_ERROR,  OnApiResultError);
		}
		
		
		protected function OnUserShowResult(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.USERS_SHOW_RESULT, OnUserShowResult);
			ObjectUtil.fill(e.result, GlobalWeiboDataModel.AccountModel);
			GlobalWeiboDataModel.AccountModel.bLogin = true;
			
			//开始获得该用户的部分微博数据
			_mb.userTimeline();
			_mb.addEventListener(WeiboServiceEvent.USER_TIMELINE_RESULT, OnUserTimeLine);
			_mb.addEventListener(WeiboServiceEvent.USER_TIMELINE_ERROR, OnApiResultError);
			
			_mb.commentsToMe();
			_mb.addEventListener(WeiboServiceEvent.COMMENTS_TO_ME_RESULT, OnCommentsToMe);
			_mb.addEventListener(WeiboServiceEvent.COMMENTS_TO_ME_ERROR, OnApiResultError);
			
		}
		
		protected function OnUserTimeLine(e:MicroBlogEvent):void
		{
			_mb.removeEventListener(WeiboServiceEvent.USER_TIMELINE_RESULT, OnUserTimeLine);
			var length:int = int(e.result["statuses"]["length"]);
			var i:int = 0;
			for(i = 0; i < length; ++i)
			{
				GlobalWeiboDataModel.StatusInfoModel._StatusSendbyMe.PushNewStatus(e.result["statuses"][i]);
			}
		}
		
	
		
		protected function OnApiResultError(e:MicroBlogErrorEvent):void
		{
			switch(e.type)
			{
				case MicroBlogErrorEvent.LOGIN_ERROR:
					GlobalWeiboDataModel.AccountModel.bLogin = false;
					break;
				
				case WeiboServiceEvent.GET_UID_ERROR:
					break;
			}
		}
	}
}